<?php
namespace frontend\widgets;

use frontend\models\EventType;
use yii\bootstrap\Widget;
use yii\behaviors\CacheableWidgetBehavior;
use yii\helpers\Inflector;

class EventsMenuForMainPage extends Widget
{
    private $eventsData;

    // public function behaviors()
    // {
    //     return [
    //         [
    //             'class' => CacheableWidgetBehavior::className(),
    //             'cacheDuration' => 3600,
    //         ],
    //     ];
    // }
    
    public function init()
    {
        parent::init();

        $this->eventsData = EventType::find()
            ->joinWith([
                'events' => function ($query) {
                    $query->select('id, id_type, date_end, date_start, id_info_chanel, is_main_event')
                    ->andWhere(['>=', 'event.date_end', date('Y-m-d')])
                    ->andWhere('event.id_info_chanel=2')
                    ->orderBy('event.date_start');

                },
            ])
            ->where('event.is_main_event=0')
            ->andWhere(['>=', 'event.date_end', date('Y-m-d')])
            ->andWhere('event.id_info_chanel=2')
            ->asArray()
            ->all();
    }

    public function run()
    {   

        if(count($this->eventsData) != 0)
        {
            echo '<div class="container events-menu-main-page-container">';
            foreach($this->eventsData as $eventType)
            {
                echo '<a href="/event/index#' . Inflector::slug($eventType['title_plural']) . '" class="base-button">' . $eventType['title_plural'] . '</a>';
            }
            echo '</div>';
        }  
        
    }


}
